package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyShoppingCard;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.service.IWdyShoppingCardService;
import com.ruoyi.system.service.IWdyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物卡 信息操作处理
 * 
 * @author ruoyi
 * @date 2020-10-09
 */
@Controller
@RequestMapping("/system/wdyShoppingCard")
public class WdyShoppingCardController extends BaseController
{
    private String prefix = "system/wdyShoppingCard";
	@Autowired
	private IWdyUserService wdyUserService;
	@Autowired
	private IWdyShoppingCardService wdyShoppingCardService;
	private static final int[] amountArray = new int[] {100, 500, 1000, 5000, 10000, 50000};
	@RequiresPermissions("system:wdyShoppingCard:view")
	@GetMapping()
	public String wdyShoppingCard()
	{
	    return prefix + "/wdyShoppingCard";
	}

	@RequiresPermissions("system:wdyShoppingCard:view")
	@GetMapping("/getCount")
	@ResponseBody
	public AjaxResult getCount() {
		List<Integer> countList = new ArrayList<>();
		for(int amount : amountArray) {
			QueryWrapper<WdyShoppingCard> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id", "");
			queryWrapper.eq("amount", amount);
			int count = wdyShoppingCardService.count(queryWrapper);
			countList.add(count);
		}
		return AjaxResult.success().put("amountArray", amountArray).put("countList", countList);
	}
 	/**
	 * 查询购物卡列表
	 */
	@RequiresPermissions("system:wdyShoppingCard:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyShoppingCard wdyShoppingCard)
	{
		startPage();
        List<WdyShoppingCard> list = wdyShoppingCardService.selectWdyShoppingCardList(wdyShoppingCard);
		for(WdyShoppingCard e : list) {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_id", e.getUserId());
			WdyUser user = wdyUserService.getOne(query);
			if(user != null) {
				e.addExtraData("mobile", user.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出购物卡列表
	 */
	@RequiresPermissions("system:wdyShoppingCard:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyShoppingCard wdyShoppingCard)
    {
    	List<WdyShoppingCard> list = wdyShoppingCardService.selectWdyShoppingCardList(wdyShoppingCard);
        ExcelUtil<WdyShoppingCard> util = new ExcelUtil<WdyShoppingCard>(WdyShoppingCard.class);
        return util.exportExcel(list, "wdyShoppingCard");
    }
	
	/**
	 * 新增购物卡
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存购物卡
	 */
	@RequiresPermissions("system:wdyShoppingCard:add")
	@Log(title = "购物卡", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyShoppingCard wdyShoppingCard)
	{		
		return toAjax(wdyShoppingCardService.insertWdyShoppingCard(wdyShoppingCard));
	}

	/**
	 * 修改购物卡
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyShoppingCard wdyShoppingCard = wdyShoppingCardService.selectWdyShoppingCardById(id);
		mmap.put("wdyShoppingCard", wdyShoppingCard);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存购物卡
	 */
	@RequiresPermissions("system:wdyShoppingCard:edit")
	@Log(title = "购物卡", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyShoppingCard wdyShoppingCard)
	{		
		return toAjax(wdyShoppingCardService.updateWdyShoppingCard(wdyShoppingCard));
	}
	
	/**
	 * 删除购物卡
	 */
	@RequiresPermissions("system:wdyShoppingCard:remove")
	@Log(title = "购物卡", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyShoppingCardService.deleteWdyShoppingCardByIds(ids));
	}


	@Log(title = "购物卡", businessType = BusinessType.IMPORT)
	@RequiresPermissions("system:wdyShoppingCard:import")
	@PostMapping("/importData")
	@ResponseBody
	public AjaxResult importData(MultipartFile file) throws Exception
	{
		ExcelUtil<WdyShoppingCard> util = new ExcelUtil<WdyShoppingCard>(WdyShoppingCard.class);
		List<WdyShoppingCard> cardList = util.importExcel(file.getInputStream());
		String operName = getSysUser().getLoginName();
		String message = wdyShoppingCardService.importCard(cardList, operName);
		return AjaxResult.success(message);
	}

	@RequiresPermissions("system:wdyShoppingCard:view")
	@GetMapping("/importTemplate")
	@ResponseBody
	public AjaxResult importTemplate()
	{
		ExcelUtil<WdyShoppingCard> util = new ExcelUtil<WdyShoppingCard>(WdyShoppingCard.class);
		return util.importTemplateExcel("导入数据");
	}


}
